package strategy;

import java.util.Comparator;

public class SortComparator<T> {
    public T [] sort(T [] arr, Comparator <T> comparator){
        for (int x = 0; x < arr.length - 1; x++) {//控制外循环次数
            for (int y = 0; y < arr.length - 1 - x; y++) {//-1是为了避免角标越界异常，-x是为了减少外循环次数
                if (comparator.compare(arr[y] , arr[y + 1]) > 0) {//判断两个相邻元素的大小
                    swap(arr, y, y + 1);
                }
            }
        }
        return arr;
    }
    public void swap(T [] arr, int a, int b){
        T temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }
}
